import * as API from '@/api/dataBoard';
import { ref, reactive, onBeforeMount, watch, onUnmounted} from 'vue';

const keys = ['weight', 'pressure', 'temp', 'frequency'];
export default () => {
  const data = ref([]);
  let timmer: any = null;
  const getProduceDevice = async () => {
    const res = await API.getProduceDevice();
    if (res) {
      data.value = res.map((item) => {
        const obj = {
          ...item,
          targetedValues: []
        }
        item.targetedValues.forEach((target) => {
          const index = keys.indexOf(target.dateTypeCode)
          if (index!==-1) {
            obj.targetedValues[index] = target;
          }
        });
        return obj;
      })
    }
  }
  onBeforeMount(() => {
    getProduceDevice();
    // 定时拉取
    // timmer = setInterval(() => {
    //   getEnvironmentCurrent();
    //   getEnvironmentAlarm();
    // },10000);
  })
  onUnmounted(() => {
    clearInterval(timmer);
  })
  return {
    data
  }
}